
package com.inepex.inechart.client;

import java.util.ArrayList;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.inepex.inechart.client.axes.AxesGWT;
import com.inepex.inechart.client.axes.TickFactoryGWT;
import com.inepex.inechart.client.cartesian.CartesianChartModuleGWT;
import com.inepex.inechart.client.displayengine.DisplayEngine;
import com.inepex.inechart.client.label.ChartTitle;
import com.inepex.inechart.client.label.GWTLabelFactory;
import com.inepex.inechart.client.label.Legend;
import com.inepex.inechart.client.linechart.LineChartGwt;
import com.inepex.inechart.shared.ChartModule;

public class IneChart extends Composite{

	private ArrayList<CartesianChartModuleGWT> cartesianChartModules;
	private ArrayList<ChartModule> chartModules;
	private IneChartEventManager eventManager;
	private DisplayEngine displayEngine;
	private AxesGWT axes;
	private GWTLabelFactory labelFactory;

	public IneChart(){
		this(Defaults.chartWidth, Defaults.chartHeight);
	}

	public IneChart(int width, int height) {
		displayEngine = new DisplayEngine(new AbsolutePanel());
		initWidget(displayEngine.getChartMainPanel());
		displayEngine.setChartSize(width, height);
		labelFactory = new GWTLabelFactory(displayEngine);
		axes = new AxesGWT(new TickFactoryGWT(), labelFactory);
		eventManager = new IneChartEventManager(this);
		
		cartesianChartModules = new ArrayList<CartesianChartModuleGWT>();
		chartModules = new ArrayList<ChartModule>();

		//event
//		addDomHandler(INJECTOR.getIneChartEventManager(), MouseDownEvent.getType());
//		addDomHandler(INJECTOR.getIneChartEventManager(), MouseUpEvent.getType());
//		addDomHandler(INJECTOR.getIneChartEventManager(), MouseMoveEvent.getType());
//		addDomHandler(INJECTOR.getIneChartEventManager(), ClickEvent.getType());
//		addDomHandler(INJECTOR.getIneChartEventManager(), MouseOutEvent.getType());
//		addDomHandler(INJECTOR.getIneChartEventManager(), MouseOverEvent.getType());

	}

	public void setSize(int width, int height){
		displayEngine.setChartSize(width, height);
	}

	public void update() {
		for (ChartModule chartModule : chartModules) {
			chartModule.update();
		}
	}


	public LineChartGwt createLineChart() {
		LineChartGwt lc = new LineChartGwt(axes, eventManager, displayEngine);
		cartesianChartModules.add(lc);
		chartModules.add(lc);
		return lc;
	}	
	
	public void setChartTitle(String title){
		setChartTitle(new ChartTitle(title));
	}

	public void setChartTitle(String title,String description){
		setChartTitle(new ChartTitle(title,description));
	}

	public void setChartTitle(ChartTitle title){
		labelFactory.setChartTitle(title);
	}

	public ChartTitle getChartTitle(){
		return labelFactory.getChartTitle();
	}

	public void setLegend(Legend legend){
		labelFactory.setLegend(legend);
	}

	public Legend getLegend(){
		return labelFactory.getLegend();
	}

	public void setEventBus(EventBus eventBus){
		eventManager.setEventBus(eventBus);
	}

	public EventBus getEventBus(){
		return eventManager.getEventBus();
	}

}
